package config

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/query"
	modelConfig "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/config"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type BanktModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func NewBankModel(ctx context.Context, db *gorm.DB) *BanktModel {
	return &BanktModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}
func (model *BanktModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (l *BanktModel) BankListPage(in *rpc.GetWithDrawBankRequest) (*rpc.GetWithDrawBankResponse, error) {

	ret := &rpc.GetWithDrawBankResponse{}
	ret.ResHead = &rpc.Head{}
	g := query.Use(l.getDb()).ConfigBank
	h := g.Where()

	if in.Id != 0 {
		h = h.Where(g.ID.Eq(in.Id))
	}
	if in.Code != "" {
		h = h.Where(g.BankCode.Eq(in.Code))
	}
	if in.Name != "" {
		h = h.Where(g.BankName.Eq(in.Name))
	}
	if in.Status != int32(rpc.WithdrawStatus_All) {
		h = h.Where(g.Status.Eq(in.Status))
	}
	var AssetsBWithdrawBank []*modelConfig.ConfigBank
	var err error
	if in.Page == nil {
		AssetsBWithdrawBank, err = h.Find()
	} else {
		if in.Page.Page <= 0 {
			in.Page.Page = 1
		}
		if in.Page.PageSize <= 0 {
			in.Page.PageSize = 10
		}
		ret.ResHead.Count, _ = h.Count()
		AssetsBWithdrawBank, _, err = h.FindByPage(int((in.Page.Page-1)*in.Page.PageSize), int(in.Page.PageSize))

	}
	if err != nil {
		return ret, err
	}

	for i, _ := range AssetsBWithdrawBank {
		ret.Data = append(ret.Data, &rpc.WithDrawBank{
			Id:       AssetsBWithdrawBank[i].ID,
			BankCode: AssetsBWithdrawBank[i].BankCode,
			BankName: AssetsBWithdrawBank[i].BankName,
			BankLogo: AssetsBWithdrawBank[i].BankLogo,
			Status:   AssetsBWithdrawBank[i].Status,
		})
	}

	return ret, nil
}
func (l *BanktModel) BankUpdate(in *rpc.UpdateWithDrawBankRequest) (*rpc.UpdateWithDrawBankResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.UpdateWithDrawBankResponse{}
	Withdraw := &modelConfig.ConfigBank{
		BankCode: in.WithDrawBank.BankCode,
		BankName: in.WithDrawBank.BankName,
		Status:   in.WithDrawBank.Status,
		BankLogo: in.WithDrawBank.BankLogo,
	}
	g := query.Use(l.getDb()).ConfigBank
	g.Where(g.ID.Eq(in.WithDrawBank.Id)).Updates(Withdraw)
	ret.Data = &rpc.WithDrawBank{
		Id:       Withdraw.ID,
		BankCode: Withdraw.BankCode,
		BankName: Withdraw.BankName,
		BankLogo: Withdraw.BankLogo,
		Status:   Withdraw.Status,
	}
	return ret, nil
}
func (l *BanktModel) BankDel(in *rpc.BankDelRequest) (*rpc.BankDelResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.BankDelResponse{}
	g := query.Use(l.getDb()).ConfigBank
	g.Where(g.ID.Eq(in.Id)).Delete(&modelConfig.ConfigBank{})
	return ret, nil
}
func (l *BanktModel) BankAdd(in *rpc.BankAddRequest) (*rpc.BankAddResponse, error) {
	// todo: add your logic here and delete this line

	ret := &rpc.BankAddResponse{}
	Withdraw := &modelConfig.ConfigBank{
		BankCode: in.WithDrawBank.BankCode,
		BankName: in.WithDrawBank.BankName,
		Status:   in.WithDrawBank.Status,
		BankLogo: in.WithDrawBank.BankLogo,
	}
	l.getDb().Create(Withdraw)
	ret.Data = &rpc.WithDrawBank{
		Id:       Withdraw.ID,
		BankCode: Withdraw.BankCode,
		BankName: Withdraw.BankName,
		BankLogo: Withdraw.BankLogo,
		Status:   Withdraw.Status,
	}
	return ret, nil
}

func (l *BanktModel) GetAllConfigBank() []modelConfig.ConfigBank {
	var res []modelConfig.ConfigBank

	dbRes := l.getDb().Unscoped().Find(&res)
	if dbRes.Error != nil {
		return res
	}

	return res
}
